package edu.upf.da.p2p.sm;

import java.lang.reflect.Constructor;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;


public class P2PMessage {

	private Message message;

	protected P2PMessage(){
		message = new Message();
		message.setThread(this.getClass().getCanonicalName());
	}

	public P2PMessage(Message m){
		this.message = m;
	}

	public static P2PMessage getMessage(Packet pktg){
		Message pkt = (Message)pktg;
		try {
			Class<?> c = Class.forName(pkt.getThread());
			Class[] cons = {Message.class};
			Constructor<?> construct =  c.getConstructor(cons);
			Object[] params = {pkt};
			return (P2PMessage) construct.newInstance(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new P2PMessage(pkt);
	}

	public synchronized void deleteProperty(String arg0) { message.deleteProperty(arg0); }

	public String getFrom() { return message.getFrom(); }

	public String getPacketID() { return message.getPacketID(); }

	public synchronized Object getProperty(String arg0) { return message.getProperty(arg0); }

	public String getSubject() { return message.getSubject();}

	public String getTo() { return message.getTo(); }

	public void setPacketID(String arg0) {message.setPacketID(arg0);}

	public synchronized void setProperty(String arg0, Object arg1) {message.setProperty(arg0, arg1); }

	public void setSubject(String arg0) {message.setSubject(arg0);}

	public void setTo(String arg0) {message.setTo(arg0);}

	public void sendMessage(P2PClient client) {
		client.sendMessage(message);
	}

}
